﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using homework2_bank;

namespace homework2_bank.classes
{
    public struct OutMoney   //出钞参数
    {
        public int BadCashNum;
        public int Money;
    }
    class ATM
    {
        /**
         * 参数
         */
        private string CurrentUser;//当前账户
        public string CurrentBank { set; get; }//当前卡片的银行
        private bool IfLogin { set; get; }//是否登陆成功
        private Bank CurrentBankInstance { set; get; }//当前银行的实体
        private DispatcherTimer timer = new DispatcherTimer();//出钞计时器
        private OutMoney OutMoneyParams;
        public delegate void BigMoneyHandler(BigMoneyFetched bigMoneyFetched);//委托
        public event BigMoneyHandler SaveBigMoney;//事件

        //创建时调用
        public ATM()
        {
            CurrentBank = "未插卡";
            IfLogin = false;
            timer.Interval = TimeSpan.FromSeconds(0.1);
            timer.Tick += Timer_Tick;
        }
        
        /**
        * 公共方法，给用户操作
        */
        //用户插卡后读卡，模拟中国银行,注册中国银行的事件
        public string ReadCard()
        {
            CurrentBankInstance = Bank.zgyg;
            CurrentBank = "中国银行";
            MainWindow.win.ScreenLabel = "读卡成功";
            MainWindow.win.BankName = CurrentBank;
            //注册事件
            this.SaveBigMoney += new ATM.BigMoneyHandler(CurrentBankInstance.BigMoneyWarn);
            return CurrentBank;
        }

        //用户登录操作
        public bool Login(string account, string password)
        {
            if (CurrentBank.Equals("未插卡"))
            {
                MainWindow.win.ScreenLabel = "请先插卡";
                return false;
            }
            else if(CurrentBankInstance.MatchAccount(account, password) != null)
            {
                CurrentUser = CurrentBankInstance.MatchAccount(account, password);
                IfLogin = true;
                MainWindow.win.ScreenLabel = "登陆成功";
                return true;
            }
            else
            {
                MainWindow.win.ScreenLabel = "登陆失败，请先检查账号密码";
                return false;
            }
        }

        //用户查询操作
        public bool GetBalance()
        {
            if (IfLogin)
            {
                int[] balanceNum = CurrentBankInstance.GetBalanceArgs(CurrentUser);
                MainWindow.win.LeftBalance = balanceNum[0];
                MainWindow.win.CreditBalance = balanceNum[1];
                MainWindow.win.AvailableBalance = balanceNum[0] + balanceNum[1];
                return true;
            }
            else
            {
                MainWindow.win.ScreenLabel = "请先插卡登录";
                return false;
            }
        }

        //用户放钱操作
        public bool MoneyIn(Cash cash)
        {
            if (IfLogin)
            {
                if (!cash.Complete)
                {
                    MainWindow.win.ScreenLabel = "放入了一张坏钞！";
                    MoneyStraightOut(cash);
                }
                else
                {
                    MainWindow.win.InsideCash += cash.Money;
                }
                return true;
            }
            else
            {
                MainWindow.win.ScreenLabel = "请先插卡登录";
                return false;
            }
        }

        //用户使用ATM向银行存钱
        public bool SaveMoney(int num)
        {
            if (IfLogin)
            {
                if (CurrentBankInstance.AddMoney(CurrentUser, num))
                {
                    MainWindow.win.InsideCash = 0;
                    GetBalance();
                    MainWindow.win.ScreenLabel = "存入成功";
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                MainWindow.win.ScreenLabel = "请先插卡登录";
                return false;
            }
        }

        //用户使用ATM取钱
        public void WithdrawalMoney(int num)
        {
            if (IfLogin)
            {
                int[] balanceNum = CurrentBankInstance.GetBalanceArgs(CurrentUser);
                int balanceLimit = balanceNum[0] + balanceNum[1];
                if (num > 10000 && num <= balanceLimit)
                {
                    BigMoneyFetched bigMoneyFetched = new BigMoneyFetched(CurrentUser, num);
                    SaveBigMoney(bigMoneyFetched);//触发事件
                    int withdrawMoneyOk = CurrentBankInstance.TakeMoney(CurrentUser, num);
                    MoneyOutBankBack(withdrawMoneyOk, num);
                }
                else if(num <= 10000 && num<= balanceLimit)
                {
                    int withdrawMoneyOk = CurrentBankInstance.TakeMoney(CurrentUser, num);
                    MoneyOutBankBack(withdrawMoneyOk, num);
                }
                else
                {
                    MainWindow.win.ScreenLabel = "余额不足";
                }
            }
            else
            {
                MainWindow.win.ScreenLabel = "请先插卡登录";
            }
        }

        /**
         * 私有函数，对用户隐形
         */

        //ATM收到坏钞后吐钱操作
        private bool MoneyStraightOut(Cash cash)
        {
            MainWindow.win.OutCash += cash.Money;
            return true;
        }

        //ATM处理银行返回的取钱参数
        private void MoneyOutBankBack(int moneyWithdrawOk, int num)
        {
            switch (moneyWithdrawOk)
            {
                case -3: MainWindow.win.ScreenLabel = "帐号不匹配";
                    break;
                case -2: MainWindow.win.ScreenLabel = "余额不足";
                    break;
                case -1: MainWindow.win.ScreenLabel = "未知错误";
                    break;
                case 0: MoneyOut(num);
                    break;
            }
        }

        //ATM按照时间间隔吐出钞票
        private void MoneyOut(int num)
        {
            MainWindow.win.ScreenLabel = "操作成功，请等待出钞完成";
            OutMoneyParams.Money = num;
            OutMoneyParams.BadCashNum = 0;
            timer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            int Money = OutMoneyParams.Money;
            try
            {
                if (Money >= 100)
                {
                    Cash cash = new Cash(100, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else if (Money >= 50)
                {
                    Cash cash = new Cash(50, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else if (Money >= 20)
                {
                    Cash cash = new Cash(20, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else if (Money >= 10)
                {
                    Cash cash = new Cash(10, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else if (Money >= 5)
                {
                    Cash cash = new Cash(5, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else if (Money > 0)
                {
                    Cash cash = new Cash(1, 1);
                    MainWindow.win.OutCash += cash.Money;
                    OutMoneyParams.Money -= cash.Money;
                }
                else
                {
                    timer.Stop();
                    string msg = "出钞完成，坏钞数：" + OutMoneyParams.BadCashNum;
                    MainWindow.win.ScreenLabel = msg;
                }
            }
            catch (BadCashException)
            {
                OutMoneyParams.BadCashNum += 1;
                Debug.WriteLine("有坏钞，坏钞数" + OutMoneyParams.BadCashNum);
            }
        }

    }

    
}
